package com.zb.automation.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Font;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.builder.ExcelWriterTableBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.zb.automation.model.request.ReqAnalysisSwaggerVO;
import com.zb.automation.model.request.ReqSwaggerDTO;
import com.zb.automation.model.response.AnalysisSwaggerDTO;
import com.zb.automation.model.response.JsonParamsDTO;
import com.zb.automation.service.AnalysisJsonService;
import com.zb.automation.util.UserAgentUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description
 * @Author ZhangBin
 * @Data 2022/7/23 14:15
 */
@Slf4j
@RequestMapping("/analysisJson")
@RestController
public class AnalysisJsonController {
    @Autowired
    private AnalysisJsonService analysisJsonService;

    /**
     * @Author ZhangBin
     * @Date 2022/7/23 14:01
     * @Description JSON 解析为 Bean
     */
    @PostMapping("/json")
    public List<JsonParamsDTO> analysisJson(@RequestBody String json){
        return analysisJsonService.analysisJson(json);
    }
    /**
     * @Author ZhangBin
     * @Date 2022/7/23 14:01
     * @Description swagger 解析为tree
     */
    @PostMapping("/swagger/tree")
    public List<AnalysisSwaggerDTO> analysisSwagger2Tree(@RequestParam("path") String path){
        return analysisJsonService.analysisSwagger2Tree(path);
    }

    /**
     * @Author ZhangBin
     * @Date 2022/7/23 14:01
     * @Description swagger 解析为List
     */
    @GetMapping("/swagger/list")
    public List<ReqSwaggerDTO> analysisSwagger2List(@RequestParam("path") String path){
        return analysisJsonService.analysisSwagger2List(path);
    }

    /**
     * @Author ZhangBin
     * @Date 2022/7/23 14:01
     * @Description 导出excel
     */
    @PostMapping("/exportExcel")
    public String exportExcel(@RequestBody List<ReqSwaggerDTO> reqSwaggerDTOS,HttpServletRequest request, HttpServletResponse response){
        try{
            return analysisJsonService.exportExcel(reqSwaggerDTOS);
        }catch (Exception e){
            log.error("导出模板失败",e);
            throw new RuntimeException("导出模板失败");
        }
    }

}
